/*
 * @Author: szx
 * @Date: 2022-03-30 14:51:34
 * @LastEditTime: 2022-03-30 14:58:23
 * @Description:
 * @FilePath: \leetcode\200-299\213\213_2.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */

var rob = function (nums) {
    if (nums.length == 1) return nums[0];
    if (nums.length == 2) return Math.max(nums[0], nums[1]);
    return Math.max(func(nums, 0, nums.length - 1), func(nums, 1, nums.length));
};
var func = function (nums, start, end) {
    let dp0 = 0;
    let dp1 = nums[start];
    let res = dp1;
    for (let i = start + 2; i <= end; i++) {
        res = Math.max(dp0 + nums[i - 1], dp1);
        dp0 = dp1;
        dp1 = res;
    }
    return res;
};
console.log(rob([2, 3, 2]));
console.log(rob([1, 2, 3, 1]));
console.log(rob([1, 2, 3]));
